package com.csit.util;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;


/**
 * 
 * @Description: List转换为Json字符串
 * @Copyright: 福州骏华信息有限公司 (c)2012
 * @Created Date : 2012-8-3
 * @author longweier
 * @vesion 1.0
 */
public class ListAndJsonUtil{
	
	private static final Map<String,Integer> map = new HashMap<String, Integer>(); 
	
	public static final String EMPTYJSON = "{\"rows\":[],\"total\":0}";
	
	static{
		map.put("java.lang.Double", 1);
		map.put("java.lang.Float", 1);
		map.put("java.lang.Byte", 1);
		map.put("java.lang.Short", 1);
		map.put("java.lang.Integer", 1);
		map.put("java.lang.Long", 1);
		map.put("java.lang.Character", 1);
		map.put("java.lang.Boolean", 1);
		map.put("java.lang.String", 1);
		map.put("java.util.Date", 1);
		
		map.put("double", 1);
		map.put("float", 1);
		map.put("byte", 1);
		map.put("short", 1);
		map.put("int", 1);
		map.put("long", 1);
		map.put("char", 1);
		map.put("boolean", 1);
		map.put("date", 1);
	}
	/**
	 * 
	 * @Description: 将List里面javabean的普通元素转换为json字符串
	 * @param
	 * @Create: 2012-8-10 上午10:40:55
	 * @author longweier
	 * @update logs
	 * @param list
	 * @return
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public static String getJSONString(List list){
		JSONObject object = new JSONObject();
		JSONArray array = new JSONArray();
		if(list==null||list.size()==0){
			return EMPTYJSON;
		}
		Field[] fields = list.get(0).getClass().getDeclaredFields();
		
		String[] propertiesName = new String[fields.length];
		
		for(int i = 0;i< fields.length;i++){
			if(fields[i].getModifiers()==2 && map.get(fields[i].getType().getName())!=null){
				propertiesName[i] = fields[i].getName();
			}
		}
		JSONObject item = null;
		try {
			for (Object t : list) {
				item = new JSONObject();
				Object value = null;
				for(String key : propertiesName){
					if(key!=null&&key.length()>0){
						value = BeanUtils.getProperty(t, key);
						item.put(key, value);
					}
				}
				array.put(item);
			}
			object.put("rows", array);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return object.toString();
	}
	
	/**
	 * 
	 * @Description: 将List里面javabean的普通元素转换为json字符串,没有加上rows
	 * @Created: 2012-9-6 上午8:58:16
	 * @Author lys
	 * @Update logs
	 * @param list
	 * @return
	 * @Throws Exception
	 */
	@SuppressWarnings("unchecked")
	public static String getJSONStringWithOutRows(List list){
		JSONArray array = new JSONArray();
		if(list==null||list.size()==0){
			return EMPTYJSON;
		}
		Field[] fields = list.get(0).getClass().getDeclaredFields();
		
		String[] propertiesName = new String[fields.length];
		
		for(int i = 0;i< fields.length;i++){
			if(fields[i].getModifiers()==2 && map.get(fields[i].getType().getName())!=null){
				propertiesName[i] = fields[i].getName();
			}
		}
		JSONObject item = null;
		try {
			for (Object t : list) {
				item = new JSONObject();
				Object value = null;
				for(String key : propertiesName){
					if(key!=null&&key.length()>0){
						value = BeanUtils.getProperty(t, key);
						item.put(key, value);
					}
				}
				array.put(item);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return array.toString();
	}
	/**
	 * 
	 * @Description: List转换为JsonObject不带分页
	 * @param
	 * @Create: 2012-8-3 下午09:50:17
	 * @author longweier
	 * @update logs
	 * @param list 数据集合
	 * @param propertiesName  属性数组
	 * @return
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public static String getJSONString(List list,String[] propertiesName){
		JSONObject object = new JSONObject();
		
		JSONArray array = new JSONArray();

		JSONObject item = null;
		try {
			for (Object t : list) {
				item = new JSONObject();
				Object value = null;
				for(String key : propertiesName){
					value = BeanUtils.getProperty(t, key);
					if(key.contains(".")){
						String propertyPrefix = StringUtils.substringBefore(key, ".");
						String propertySuffix = StringUtils.substringAfter(key, ".");
						if(BeanUtils.getProperty(t, propertyPrefix)!=null){
							item.put(propertySuffix, value);
						}
					}else{
						item.put(key, value);
					}
				}
				array.put(item);
			}
			object.put("rows", array);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return object.toString();
	}
	
	/**
	 * 
	 * @Description: 将List里面javabean的普通元素转换为json字符串,没有加上rows
	 * @Created: 2012-9-6 上午8:58:16
	 * @Author lys
	 * @Update logs
	 * @param list
	 * @return
	 * @Throws Exception
	 */
	@SuppressWarnings("unchecked")
	public static String getJSONStringWithOutRows(List list,String[] propertiesName){
		JSONArray array = new JSONArray();

		JSONObject item = null;
		try {
			for (Object t : list) {
				item = new JSONObject();
				Object value = null;
				for(String key : propertiesName){
					value = BeanUtils.getProperty(t, key);
					if(key.contains(".")){
						String propertyPrefix = StringUtils.substringBefore(key, ".");
						String propertySuffix = StringUtils.substringAfter(key, ".");
						if(BeanUtils.getProperty(t, propertyPrefix)!=null){
							item.put(propertySuffix, value);
						}
					}else{
						item.put(key, value);
					}
				}
				array.put(item);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return array.toString();
	}
	/**
	 * 
	 * @Description: List转换为JsonObject 带分页
	 * @param
	 * @Create: 2012-8-3 下午09:55:43
	 * @author longweier
	 * @update logs
	 * @param list 数据集合
	 * @param propertiesName 属性数组
	 * @param totalCount  总页数
	 * @return
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public static String getJSONString(List list,String[] propertiesName,Long totalCount){
		if(list==null||list.size()==0){
			return EMPTYJSON;
		}
		JSONObject object = new JSONObject();
		JSONArray array = new JSONArray();
		JSONObject item = null;
		try {
			for (Object t : list) {
				item = new JSONObject();
				Object value = null;
				for(String key : propertiesName){
					value = BeanUtils.getProperty(t, key);
					if(key.contains(".")){
						String propertyPrefix = StringUtils.substringBefore(key, ".");
						String propertySuffix = StringUtils.substringAfter(key, ".");
						if(BeanUtils.getProperty(t, propertyPrefix)!=null){
							item.put(propertySuffix, value);
						}
					}else{
						item.put(key, value);
					}
				}
				array.put(item);
			}
			object.put("rows", array);
			object.put("total",totalCount==null?0:totalCount);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return object.toString();
	}
	
	/**
	 * 
	 * @Description: 把List<Map<String,Object>>转换为json字符串
	 * @param
	 * @Create: 2012-8-15 下午05:36:08
	 * @author longweier
	 * @update logs
	 * @param listMap
	 * @return
	 * @return
	 * @throws Exception
	 */
	public static String getJSONStringFromListMap(List<Map<String,Object>> listMap){
		if(listMap==null||listMap.size()==0){
			return EMPTYJSON;
		}
		JSONObject object = new JSONObject();
		JSONArray array = new JSONArray();
		JSONObject item = null;
		Set<String> keySet = listMap.get(0).keySet();
		try {
			for (Map<String,Object> map : listMap) {
				item = new JSONObject();
				Object value = null;
				for(String key : keySet){
					value = map.get(key);
					item.put(key, value);
				}
				array.put(item);
			}
			object.put("rows", array);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return object.toString();
	}
	
	/**
	 * 
	 * @Description: 把List<Map<String,Object>>转换为json字符串,带有分页信息
	 * @param
	 * @Create: 2012-8-27 下午04:49:30
	 * @author longweier
	 * @update logs
	 * @param listMap
	 * @param totalCount
	 * @return
	 * @return
	 * @throws Exception
	 */
	public static String getJSONStringFromListMap(List<Map<String,Object>> listMap, Long totalCount){
		if(listMap==null||listMap.size()==0){
			return EMPTYJSON;
		}
		JSONObject object = new JSONObject();
		JSONArray array = new JSONArray();
		JSONObject item = null;
		Set<String> keySet = listMap.get(0).keySet();
		try {
			for (Map<String,Object> map : listMap) {
				item = new JSONObject();
				Object value = null;
				for(String key : keySet){
					value = map.get(key);
					item.put(key, value);
				}
				array.put(item);
			}
			object.put("rows", array);
			object.put("total",totalCount==null?0:totalCount);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return object.toString();
	}
	
}
